<?php

namespace app\publics\controller;

use think\Controller;

Class Upload extends Controller
{
    // 文件上传
    public function index()
    {
        // 接收数据进行上传分类
        $type = \think\facade\Request::post('type');
        $path = './uploads/'.$type;


        $file = request()->file('file');
        $info = $file->validate(['size'=>500*1024, 'ext'=>'jpg,png,gif'])->move($path);

        if ($info) {
            return json([
                'code' => 0,
                'msg'  => '',
                'data' => [
                    'src' => '/uploads/'.$type.'/'.$info->getSaveName()
                ]
            ]);
        }

        return json([
            'code' => -1,
            'msg' => '图片上传失败,图片大小最大为500kb',
            'data' => $file->getError()
        ]);
    }
}